<?
function drizzo_session_check() {
	session_name('drizzo_session');
	session_start();
	if(($_SESSION['prev_ip'] != $_SERVER['REMOTE_ADDR']) && isset($_POST)) {
		unset($_POST);
		unset($_GET);
	}
	$_SESSION['prev_ip'] = $_SERVER['REMOTE_ADDR'];
}

function drizzo_protector() {
	if(drizzo_login_check()) {
		return true;
	} else {
		include_once(admin_folder . "/engine/login_inc.php");
		include_once(admin_folder . "/footer.php");
		exit();
	}
}

function drizzo_pages_table($status = null) {
	echo "<table id=\"page_posts\">\n";
	echo "\t<tr id=\"first\">\n";
	echo "\t\t<th scope=\"col\" class=\"center\">ID</th><th scope=\"col\">Title</th><th scope=\"col\">Tags</th><th scope=\"col\">Comments</th><th scope=\"col\" class=\"center\">Author</th><th scope=\"col\" colspan=\"3\" class=\"center\">Action</th>\n";
	echo "\t</tr>\n";
	drizzo_page_list_backend(0,0,$status);
	echo "</table>";
}

function drizzo_page_list_backend($parent = 0, $sub_no = 0, $status = null) {
	drizzo_db_connect();

	global $drizzo_posts, $drizzo_users, $drizzo_comments, $row_count, $drizzo_admin_images;

	if(!$status) {
		$page_query = @mysql_query("SELECT `ID` FROM `$drizzo_posts` WHERE `status`!='draft' AND `page_parent`='$parent' AND `type`='page' ORDER BY `page_order`,`ID` ASC");
		if(mysql_errno()) {
			echo "\t<tr>\n\t\t<td colspan=\"8\"><p>No Pages were found.</p></td>\n\t</tr>\n";
		} else {
			while($row = @mysql_fetch_assoc($page_query)) {
				$alt_check = ($alt & 1);
				if($alt_check == 0) { $alt_class = "alt"; $alt++; } else { $alt_class = ""; $alt--; }

				$the_post = drizzo_get_the_post($row['ID']);

				if($sub_no > 0) { $sub = "sub" . $sub_no; } else { $sub = ""; }

				if($the_post['page_parent'] == $parent) {

					if ($row_count%2) { $alt = "alt"; } else { $alt = ""; }

					echo "<tr class=\"" . $alt . "\">\n";
					echo "\t<td class=\"center\" width=\"1\"><strong>" . $the_post['ID'] . "</strong></td><td class=\"" . $sub . "\">" . $the_post['title'] . "</td><td>" . drizzo_post_tags($the_post['ID']) . "</td><td class=\"center\" width=\"1\">" . $the_post['comment_count'] . "</td><td class=\"center\" width=\"1\">" . drizzo_post_author('nice_name',$the_post['ID']) . "</td><td class=\"center\" width=\"1\">" . drizzo_view_link_backend() . "</td><td class=\"center\" width=\"1\">" . drizzo_edit_link_backend($the_post['ID']) . "</td><td class=\"center\" width=\"1\">" . drizzo_delete_link_backend($the_post['ID']) . "</td>\n";
					echo "</tr>\n";

					$row_count++;

					drizzo_page_list_backend($the_post['ID'], $sub_no +1);
				}
			}
		}
	}

	if($status) {
		$page_query = @mysql_query("SELECT * FROM `$drizzo_posts` WHERE `status`='$status' AND `type`='page' ORDER BY `ID` DESC");
		if(mysql_errno()) {
			echo "\t<tr>\n\t\t<td colspan=\"8\"><p>No Pages were found.</p></td>\n\t</tr>\n";
		} else {

			while($row = @mysql_fetch_assoc($page_query)) {
				$alt_check = ($alt & 1);
				if($alt_check == 0) { $alt_class = "alt"; $alt++; } else { $alt_class = ""; $alt--; }

				$the_post = drizzo_get_the_post($row['ID']);

				if($sub_no > 0) { $sub = "sub" . $sub_no; } else { $sub = ""; }

				if($the_post['page_parent'] == $parent) {

					if ($row_count%2) { $alt = "alt"; } else { $alt = ""; }

					echo "<tr class=\"" . $alt . "\">\n";
					echo "\t<td class=\"center\" width=\"1\"><strong>" . $the_post['ID'] . "</strong></td><td class=\"" . $sub . "\">" . $the_post['title'] . "</td><td>" . drizzo_post_tags($the_post['ID']) . "</td><td class=\"center\" width=\"1\">" . $the_post['comment_count'] . "</td><td class=\"center\" width=\"1\">" . drizzo_post_author('nice_name',$the_post['ID']) . "</td><td class=\"center\" width=\"1\">" . drizzo_view_link_backend() . "</td><td class=\"center\" width=\"1\">" . drizzo_edit_link_backend($the_post['ID']) . "</td><td class=\"center\" width=\"1\">" . drizzo_delete_link_backend($the_post['ID']) . "</td>\n";
					echo "</tr>\n";

					$row_count++;

					drizzo_page_list_backend($the_post['ID'], $sub_no +1);
				}
			}
		}
	}
}

function drizzo_post_authors_dropdown($author = null) {
	global $the_post, $drizzo_users, $the_event;
	
	drizzo_db_connect();
	
	echo "<h3>Author</h3>\n";
	
	if($the_post) {
		$author = $the_post['author'];
	} elseif($the_event) {
		$author = $the_event['author'];
	}
	
	$authors_lookup = @mysql_query("SELECT `ID`,`nice_name`,`username` FROM `$drizzo_users` WHERE `user_level`!='0'");
	if(!mysql_errno()) {
		echo "<select name=\"author\">\n";
		while($row = @mysql_fetch_assoc($authors_lookup)) {
			$author_id = "";
			$author_nicename = "";
			
			$author_id = $row['ID'];
			$author_nicename = $row['nice_name'];
			if(!$author_nicename) { $author_nicename = $row['username']; }
			if($author == $author_id) {
				echo "\t<option value=\"$author_id\" selected>" . $author_nicename ."</option>\n";
			} else {
				echo "\t<option value=\"$author_id\">" . $author_nicename ."</option>\n";
			}
		}
		echo "</select>\n";
	}
}

function drizzo_types_dropdown() {
	global $the_post, $drizzo_posts;
		
	drizzo_db_connect();
	
	echo "<h3>Type</h3>\n";

	if(!$the_post) {
		$preselected_type = pageid;
		if(pageid == "pages") {
			$preselected_type = "page";
		}
		$type = $preselected_type;
	} else {
		$type = $the_post['type'];
	}
	
	$types_lookup = @mysql_query("SHOW COLUMNS FROM `$drizzo_posts` LIKE '%type%'");
	if(!mysql_errno()) {
		echo "<select name=\"type\">\n";
		$types_array = @mysql_fetch_assoc($types_lookup);
		preg_match_all("/'(.*?)'/", $types_array['Type'], $matches);
		$type_count = count($matches[0]);
		for($i = 0; $i < $type_count; $i++) {
			$types = preg_replace("/\'/", "", $matches[0][$i]);
			if($types == $type) {
				echo "\t<option value=\"" . $types . "\" selected>" . ucwords($types) . "</option>\n";
			} else {
				echo "\t<option value=\"" . $types . "\">" . ucwords($types) . "</option>\n";
			}
		}
		echo "</select>\n";
	} else {
		echo "SQL can suck my ass.";
	}	
}

function drizzo_post_statuses_dropdown() {
	global $the_post, $drizzo_posts;
	
	drizzo_db_connect();
	
	echo "<h3>Status</h3>\n";

	if(!$the_post) {
		$status = "draft";
	} else {
		$status = $the_post['status'];
	}
	
	$statuses_lookup = @mysql_query("SHOW COLUMNS FROM `$drizzo_posts` LIKE '%status%'");
	if(!mysql_errno()) {
		echo "<select name=\"status\">\n";
		$statuses_array = @mysql_fetch_assoc($statuses_lookup);
		preg_match_all("/'(.*?)'/", $statuses_array['Type'], $matches);
		$status_count = count($matches[0]);
		for($i = 0; $i < $status_count; $i++) {
			$statuses = preg_replace("/\'/", "", $matches[0][$i]);
			if($statuses == $status) {
				echo "\t<option value=\"" . $statuses . "\" selected>" . ucwords($statuses) . "</option>\n";
			} else {
				echo "\t<option value=\"" . $statuses . "\">" . ucwords($statuses) . "</option>\n";
			}
		}
		echo "</select>\n";
	} else {
		echo "SQL can suck my ass.";
	}	
}

function drizzo_comment_statuses_dropdown() {
	global $comment, $drizzo_comments;
	
	drizzo_db_connect();
	
	$string = "<h3>Status</h3>\n";

	if(!$comment) {
		$status = "moderated";
	} else {
		$status = $comment['status'];
	}
	
	$statuses_lookup = @mysql_query("SHOW COLUMNS FROM `$drizzo_comments` LIKE '%status%'");
	if(!mysql_errno()) {
		$string .= "<select name=\"status\">\n";
		$statuses_array = @mysql_fetch_assoc($statuses_lookup);
		preg_match_all("/'(.*?)'/", $statuses_array['Type'], $matches);
		$status_count = count($matches[0]);
		for($i = 0; $i < $status_count; $i++) {
			$statuses = preg_replace("/\'/", "", $matches[0][$i]);
			if($statuses == $status) {
				$string .= "\t<option value=\"" . $statuses . "\" selected>" . ucwords($statuses) . "</option>\n";
			} else {
				$string .= "\t<option value=\"" . $statuses . "\">" . ucwords($statuses) . "</option>\n";
			}
		}
		$string .= "</select>\n";
	} else {
		$string = false;
	}
	return $string;
}

function drizzo_post_parents_dropdown() {
	echo "<h3>Page Parent</h3>\n";
	echo "<select name=\"page_parent\">\n";
	echo "\t<option value=\"0\"></option>";
	drizzo_post_parents_dropdown_loop();
	echo "</select>\n";
}

function drizzo_post_parents_dropdown_loop($parents = 0, $sub_no = 0) {
	global $drizzo_posts, $the_post;

	$page_query = @mysql_query("SELECT * FROM `$drizzo_posts` WHERE `status`!='draft' AND `page_parent`='$parents' AND `type`='page' ORDER BY `page_order`,`ID` ASC");
	
	if(mysql_errno()) {
		$parent_id = 0;
	} else {
		$parent_id = $the_post['page_parent'];
	}
	while($row = @mysql_fetch_assoc($page_query)) {

		$id = "";
		$title = "";
		$page_parent = "";

		$id = $row['ID'];
		$title = $row['title'];
		$title = str_pad($title, (strlen($title) + $sub_no), "-", STR_PAD_LEFT);
		if(strlen($title) > 25) { $title = substr($title, 0, 22) . "..."; }
		$page_parent = $row['page_parent'];

		if($page_parent == $parents) {
			
			if($id == $parent_id && $id != $the_post['ID']) {
				echo "\t<option value=\"$id\" selected>" . $title . "</option>\n";
			} elseif($id != $the_post['ID']) {
				echo "\t<option value=\"$id\">" . $title . "</option>\n";
			}

			
			drizzo_post_parents_dropdown_loop($id, $sub_no +1);
		}
	}
}

function drizzo_post_comment_statuses_dropdown() {
	global $the_post, $drizzo_posts;
	
	drizzo_db_connect();
	
	echo "<h3>Comment Status</h3>\n";

	if(!$the_post) {
		$status = 0;
	} else {
		$status = $the_post['comment_status'];
	}
	
	$statuses_lookup = @mysql_query("SHOW COLUMNS FROM `$drizzo_posts` LIKE '%comment_status%'");
	if(!mysql_errno()) {
		echo "<select name=\"comment_status\">\n";
		$statuses_array = @mysql_fetch_assoc($statuses_lookup);
		preg_match_all("/'(.*?)'/", $statuses_array['Type'], $matches);
		$status_count = count($matches[0]);
		for($i = 0; $i < $status_count; $i++) {
			$statuses = preg_replace("/\'/", "", $matches[0][$i]);
			if($statuses == $status) {
				echo "\t<option value=\"" . $statuses . "\" selected>" . ucwords($statuses) . "</option>\n";
			} else {
				echo "\t<option value=\"" . $statuses . "\">" . ucwords($statuses) . "</option>\n";
			}
		}
		echo "</select>\n";
	} else {
		echo "SQL can suck my ass.";
	}	
}

function drizzo_edit_status() {
	global $the_post, $drizzo_users, $action;
	
	if($action != "new") {
		echo "<h3>Last Edited</h3>\n";

		$editor = $the_post['edited_by'];
		$edit_date = $the_post['edited_date'];
		if($editor == 0) {
			$editor_nice_name = "unknown user";
		} else {
			$editor_query = @mysql_query("SELECT `nice_name`,`username` FROM `$drizzo_users` WHERE `ID`='$editor'");
			if(!mysql_errno()) {
				while($row = @mysql_fetch_assoc($editor_query)) {
					$editor_nice_name = $row['nice_name'];
					if(empty($editor_nice_name)) {
						$editor_nice_name = $row['username'];
					}
				}
			}
		}

		echo "<p>Last edited by " . $editor_nice_name . " on " . $edit_date . "</p>";
	} else {
		return false;
	}

}

function drizzo_menu_name_input() {
	global $the_post;

	echo "<h3>Menu Name</h3>\n";
	echo "<input type=\"text\" name=\"menu_name\" value=\"" . $the_post['menu_name'] . "\"></input>\n";
}

function drizzo_page_order_input() {
	global $the_post;

	echo "<h3>Page Order <input type=\"text\" name=\"page_order\" id=\"page_order\" value=\"" . $the_post['page_order'] . "\"></input></h3>\n";
}

function drizzo_post_permalink_name_input() {
	global $the_post;

	echo "<h3>Permalink</h3>\n";
	echo "<input type=\"text\" name=\"permalink_name\" value=\"" . $the_post['permalink_name'] . "\"></input>\n";
}

function drizzo_post_eval_content_dropdown() {
	global $the_post;
	if(drizzo_global_options('allow_eval') == true) {
		echo "<h3>Evaluate PHP Content</h3>\n";
		echo "<select name=\"eval_content\">";
		if(drizzo_global_options('post_eval_' . $the_post['ID']) == 1) {
			echo "<option value=\"1\" selected>Yes</option>";
		} else {
			echo "<option value=\"1\">Yes</option>";
		}
		if(drizzo_global_options('post_eval_' . $the_post['ID']) == 0 || !drizzo_global_options('post_eval_' . $the_post['ID'])) {
			echo "<option value=\"0\" selected>No</option>";
		} else {
			echo "<option value=\"0\">No</option>";
		}
		echo "</select><br />\n";
	}
}

function drizzo_load_editor() {
	global $drizzo_options;
	$editor = drizzo_user_options('editor');
	if($editor == "plaintext") {
		
	}
}

function drizzo_recent_tags($limit = 10) {
	global $drizzo_posts;
	
	drizzo_db_connect;
	$tags_query = @mysql_query("SELECT `tags` FROM `$drizzo_posts` ORDER BY `ID` DESC");
	if(!mysql_errno()) {
		while($row = @mysql_fetch_assoc($tags_query)) {
			$tags = $row['tags'];
			$tag = explode(", ", $tags);
			$tag_count = count($tag);
			for($i = 0; $i < $tag_count; $i++) {
				if($tag[$i] != null) {
					$tags_list[] .= $tag[$i];
				}
			}
		}

		$tag_count = 0;
		for($i = 0; $i < count($tags_list); $i++) {
			$prev = $i - 1;
			if($tags_list[$i] != $tags_list[$prev] && $tag_count < $limit) {
				$tags_string[] .= $tags_list[$i];
				$tag_count++;
			}
		}
		if(count($tags_string) > 0) {
			$tags_lower = array_map('strtolower', $tags_string);
			@array_multisort($tags_lower, SORT_ASC, SORT_STRING, $tags_string);
		}

		if(count($tags_string) > 1) {
			$tag_count = 1;
			foreach($tags_string as $tag) {
				if($tag_count < count($tags_string)) {
					$all_tags .= "<a href=\"javascript:void(0)\" onclick=\"AddTheTag('$tag')\">" . preg_replace("/-/", " ", $tag) . "</a>, ";
				} else {
					$all_tags .= "<a href=\"javascript:void(0)\" onclick=\"AddTheTag('$tag')\">" . preg_replace("/-/", " ", $tag) . "</a>";
				}
				$tag_count++;
			}
		} elseif(count($tags_string) == 1) {
			$all_tags = "<a href=\"javascript:void(0)\" onclick=\"AddTheTag('$tag')\">" . preg_replace("/-/", " ", $tags_string[0]) . "</a>";
		} else {
			$all_tags = "<p>You have not used any tags yet</p>";
		}

		return $all_tags;

	} else {
		return false;
	}
}

function drizzo_drafts_count($types = "blog,aside") {
	global $drizzo_posts;
	$drafts_count = 0;
	$current_user = session_user_id;

	$type_list = explode(",", $types);
	for($i =0; $i < count($type_list); $i++) {
		if($i < (count($type_list) -1)) {
			$type_string .= "'" . $type_list[$i] . "', ";
		} else {
			$type_string .= "'" . $type_list[$i] . "'";
		}
	}
	
	$types = $type_string;
	
	$drafts_number = @mysql_query("SELECT * FROM `$drizzo_posts` WHERE `status`='draft' AND `type` IN ($types)");
	if(!mysql_errno()) {
		while($row = @mysql_fetch_assoc($drafts_number)) {
			$the_post['author'] = $row['author'];
			if($current_user == $the_post['author']) {
				$drafts_count++;
			} elseif(drizzo_post_permissions_check()) {
				$drafts_count++;
			} else {
				$drafts_count = $drafts_count;
			}
		}
	} else {
		$drafts_count = "0";
	}
	return $drafts_count;
}

function drizzo_post_permissions_check($id = null) {
	global $the_post;
	
	if($id != null) {
		$the_post = drizzo_get_the_post($id);
	}
	if((session_user_id == $the_post['author']) || ($the_post['type'] == 'blog' && drizzo_user_is_allowed('blog-publish')) || ($the_post['type'] == 'page' && drizzo_user_is_allowed('pages-publish'))) {
		return true;
	} else {
		return false;
	}
}

function drizzo_view_link_backend($id = null) {
	global $the_post;
	if($id != null) {
		$the_post = drizzo_get_the_post($id);
	}
	$link_string = "<a href=\"" . drizzo_post_permalink($the_post['ID']) . "\" title=\"View This Post\" class=\"view_link\"><span>View</span></a>";
	
	return $link_string;
}

function drizzo_edit_link_backend($id = null) {
	global $the_post;
	
	if($id == null) {
		$id = $the_post['ID'];
	}
	
	if(drizzo_post_permissions_check()) {
		$link_string = "<a href=\"/drizzo/" . pageid . ".php?edit=" . $id ."\" title=\"Edit This Post\" class=\"edit_link\"><span>Edit</span></a>";
	} else {
		$link_string = "<a href=\"\" title=\"You do not have permission to edit this Post\" class=\"edit_link_off\"><span><del>Edit</del></span></a>";
	}
	
	return $link_string;
}

function drizzo_edit_link($before = "[ ", $after = " ]") {
	global $the_post, $the_event, $comment;
	$type = $the_post['type'];
	$link_string = null;
	if($type == 'blog' && drizzo_post_permissions_check($the_post['ID'])) {
		$link_string = $before . "<a href=\"" . admin_folder . "blog.php?edit=" . $the_post['ID'] ."\" title=\"Edit This Post\">Edit</a>" . $after;
	} elseif($type == 'page' && drizzo_post_permissions_check($the_post['ID'])) {
		$link_string = $before . "<a href=\"" . admin_folder . "pages.php?edit=" . $the_post['ID'] ."\" title=\"Edit This Post\">Edit</a>" . $after;
	} elseif($comment && (drizzo_user_is_allowed('comments_moderate') || $comment['author_id'] == session_user_id)) {
		$link_string = $before . "<a href=\"" . admin_folder . "comments.php?edit=" . $comment['ID'] ."\" title=\"Edit This Comment\">Edit</a>" . $after;
	} elseif($the_event && drizzo_event_permissions_check()) {
		$link_string = $before . "<a href=\"" . admin_folder . "events.php?action=edit&amp;id=" . $the_event['ID'] ."\" title=\"Edit This Post\">Edit</a>" . $after;
	}
	
	return $link_string;
}

function drizzo_delete_link_backend($id = null) {
	global $the_post;

	if($id == null) {
		$id = $the_post['ID'];
	}
	
	if(drizzo_post_permissions_check()) {
			$link_string = "<a href=\"/drizzo/" . pageid . ".php?delete=" . $id ."\" title=\"Delete This Post\" class=\"delete_link\"><span>Delete</span></a>";
		} else {
			$link_string = "<a href=\"\" title=\"You do not have permission to delete this Post\" class=\"delete_link_off\"><span><del>Delete</del></span></a>";
		}
	
	return $link_string;
}

function drizzo_swap_sort_direction($order_by = null) {
	if($_GET['sort'] == $order_by && $_GET['direction'] == 'ASC') {
		$direction = 'DESC';
	} elseif($_GET['sort'] == '$order_by' && $_GET['direction'] == 'DESC') {
		$direction = "ASC";
	} elseif(!$_GET['sort'] && $order_by == 'ID') {
		$direction = "DESC";
	} else {
		$direction = "ASC";
	}
	return $direction;
}

function drizzo_up_or_down($order_by = null) {
	if($order_by != null) {
		if($_GET['sort'] == $order_by) {
			if($_GET['direction'] == 'ASC') {
				$arrow = "up";
			} else {
				$arrow = "down";
			}
		} elseif($order_by == 'ID' && !$_GET['direction']) {
			$arrow = "up";
		}
	} else {
		$arrow = false;
	}
	return $arrow;
}

function drizzo_parse_referer() {
	$old_url = pathinfo($_SERVER['HTTP_REFERER']);
	$old_url = explode('?', $old_url['basename']);
	$old_script = $old_url[0];
	$old_get = $old_url[1];
	if($old_get) {
		$get_vars = explode('&', $old_get);
		foreach($get_vars as $get_var) {
			$get_var = explode('=', $get_var);
			$key = $get_var[0];
			$var = $get_var[1];
			$get_vars_parsed[$key] = $var;
		}
	}
	return $get_vars_parsed;
}

function drizzo_parse_the_tags($tags) {
	$tags = drizzo_db_wrap(rtrim($tags,', '));
	$tags = explode(', ', $tags);
	$new_tags = null;
	$tag_count = 1;
	foreach($tags as $tag) {
		if($tag_count < count($tags) && $tag != ' ') {
			$new_tags .= str_replace(' ','-',$tag) . ", ";
		} elseif($tag != ' ') {
			$new_tags .= str_replace(' ','-',$tag);
		}
		$tag_count++;
	}
	return $new_tags;
}

function drizzo_validate_url($url) {
	$url_curl = curl_init($url);
	curl_setopt($url_curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($url_curl, CURLOPT_HEADER, 0);
	$status = curl_exec($url_curl);
	curl_close($url_curl);

	if($status) {
		return true;
	} else {
		return false;
	}
}

function drizzo_base_tips() {
	return false;
}

function drizzo_latest_activity($limit = 10) {
	global $drizzo_comments, $drizzo_posts, $drizzo_events;
	
	drizzo_db_connect();
	$comments_query = @mysql_query("SELECT `ID`,`author_id`,`author_name`,`date`,`post_id` FROM `$drizzo_comments` WHERE `status`='approved' ORDER BY `ID` LIMIT $limit");
	if(!mysql_errno()) {
		while($comment = @mysql_fetch_assoc($comments_query)) {
			$items[$comment['date']] = drizzo_db_unwrap($comment);
			$items[$comment['date']]['item_type'] = 'comment';
		}
	}
	$events_query = @mysql_query("SELECT `ID`,`what`,`edited_date`,`added_by` FROM `$drizzo_events` WHERE `status`='publish' ORDER BY `ID` DESC LIMIT $limit");
	if(!mysql_errno()) {
		while($event = @mysql_fetch_assoc($events_query)) {
			$items[$event['edited_date']] = $event;
			$items[$event['edited_date']]['item_type'] = 'event';
		}
	}
	$posts_query = @mysql_query("SELECT `ID`,`title`,`posted_date`,`type` FROM `$drizzo_posts` WHERE `status`='publish' ORDER BY `ID` DESC LIMIT $limit");
	if(!mysql_errno()) {
		while($post = @mysql_fetch_assoc($posts_query)) {
			$items[$post['posted_date']] = drizzo_db_unwrap($post);
			if($post['type'] == 'image') {
				$items[$post['posted_date']]['item_type'] = 'image';
			} else {
				$items[$post['posted_date']]['item_type'] = 'post';
			}
		}
	}
	if(count($items) > 0) {
		krsort($items);
		$list = "<ul id=\"recent_activity\">\n";
		foreach($items as $item) {
			if($i < $limit) {
				if($item['item_type'] == 'post') {
					$list .= "\t<li>Post: <a href=\"" . drizzo_post_permalink($item['ID']) . "\" title=\"$item[title]\">$item[title]</a>";
					if(drizzo_post_permissions_check($item['ID'])) {
						$list .= " (<a href=\"/drizzo/$item[type].php?edit=$item[ID]\" title=\"Edit this Post\">Edit</a>)";
					}
					$list .= "</li>\n";
				} elseif($item['item_type'] == 'comment' && $item['author_id'] != 0) {
					$list .= "\t<li>Comment: " . drizzo_user_info($item['author_id'], 'nice_name') . " on <a href=\"" . drizzo_post_permalink($item['post_id']) . "#comments-$item[ID]\" title=\"View this Comment\">" . drizzo_post_title($item['post_id']) . "</a>";
					if(drizzo_user_is_allowed('comments-moderate')) {
						$list .= " (<a href=\"/drizzo/comments.php?edit=$item[ID]\" title=\"Edit this Comment\">Edit</a>)";
					}
					$list .= "</li>\n";
				} elseif($item['item_type'] == 'comment') {
					$list .= "\t<li>Comment: $item[author_name] on <a href=\"" . drizzo_post_permalink($item['post_id']) . "#comments-$item[ID]\" title=\"View this Comment\">" . drizzo_post_title($item['post_id']) . "</a>";
					if(drizzo_user_is_allowed('comments-moderate')) {
						$list .= " (<a href=\"/drizzo/comments.php?edit=$item[ID]\" title=\"Edit this Comment\">Edit</a>)";
					}
					$list .= "</li>\n";
				} elseif($item['item_type'] == 'image') {
					$list .= "\t<li>Image: <a href=\"" . drizzo_gallery_image_permalink($item['ID']) . "\" title=\"$item[title]\">$item[title]</a>";
					if(drizzo_post_permissions_check($item['ID'])) {
						$list .= " (<a href=\"/drizzo/gallery.php?edit=$item[ID]\" title=\"Edit this Image\">Edit</a>)";
					}
					$list .= "</li>\n";
				} elseif($item['item_type'] == 'event') {
					$list .= "\t<li>Event: <a href=\"\" title=\"$item[what]\">$item[what]</a>";
					if(drizzo_user_is_allowed('events-publish') || $item['added_by'] == session_user_id) {
						$list .= " (<a href=\"/drizzo/events.php?action=edit&amp;id=$item[ID]\" title=\"Edit this Event\">Edit</a>)";
					}
				}
				$i++;
			}
			
		}
		$list .= "</ul>\n";
		return $list;
	} else {
		return false;
	}
}

function drizzo_date_dropdowns($name_prefix = null) {
	global $the_post, $the_event;
	$offset = drizzo_global_options('timezone_offset') + date('I');
	if($the_event) {
		if($name_prefix == 'start_') {
			$the_post['posted_date_raw'] = $the_event['start_raw'];
		} elseif($name_prefix == 'end_') {
			$the_post['posted_date_raw'] = $the_event['end_raw'];
		}
	}
	// The Month
	$dropdowns = "<select name=\"" . $name_prefix . "date_month\" class=\"date_select\" id=\"" . $name_prefix . "date_month_dropdown\" onchange=\"updateDateDropdowns('$name_prefix')\">";
	for($i = 1; $i <= 12; $i++) {
		$i = str_pad($i, 2, "0", STR_PAD_LEFT);
		$m = gmdate('F', gmmktime(gmdate('H') + $offset, gmdate('i'), gmdate('s'), $i, gmdate('d'), gmdate('Y')));
		if(gmdate('m', $the_post['posted_date_raw']) == $i || (gmdate('m') == $i && !$the_post)) {
			$dropdowns .= "<option value =\"$i\" selected>$m</option>";
		} else {
			$dropdowns .= "<option value =\"$i\">$m</option>";
		}
	}
	$dropdowns .= "</select>\n";
	// The Day
	$dropdowns .= "<span id=\"" . $name_prefix . "date_days\"><select name=\"" . $name_prefix . "date_day\" id=\"" . $name_prefix . "date_day_dropdown\" class=\"date_select\">";
	for($i = 1; $i <= gmdate('t', gmmktime(gmdate('H') + $offset, gmdate('i'), gmdate('s'), $i, gmdate('d'), gmdate('Y'))); $i++) {
		$i = str_pad($i, 2, "0", STR_PAD_LEFT);
		$d = gmdate('j', gmmktime(gmdate('H') + $offset, gmdate('i'), gmdate('s'), gmdate('m'), $i, gmdate('Y')));
		if(gmdate('d', $the_post['posted_date_raw']) == $i || (gmdate('d') == $i && !$the_post)) {
			$dropdowns .= "<option value =\"$i\" selected>$d</option>";
		} else {
			$dropdowns .= "<option value =\"$i\">$d</option>";
		}
	}
	$dropdowns .= "</select></span>\n";
	// The Year
	$dropdowns .= "<select name=\"" . $name_prefix . "date_year\" id=\"" . $name_prefix . "date_year_dropdown\" class=\"date_select\">";
	for($i = 2000; $i <= gmdate('Y') + 2; $i++) {
		$y = gmdate('Y', gmmktime(gmdate('H') + $offset, gmdate('i'), gmdate('s'), gmdate('m'), gmdate('d'), $i));
		if(gmdate('Y', $the_post['posted_date_raw']) == $i || (gmdate('Y') == $i && !$the_post)) {
			$dropdowns .= "<option value =\"$i\" selected>$y</option>";
		} else {
			$dropdowns .= "<option value =\"$i\">$y</option>";
		}
	}
	$dropdowns .= "</select>\n";
	$dropdowns .= " &nbsp; ";
	// The Hour
	$dropdowns .= "<span id=\"" . $name_prefix . "date_hour\"><select name=\"" . $name_prefix . "date_hour\" id=\"" . $name_prefix . "date_hour_dropdown\" class=\"date_select\">";
	$midnight = gmdate('G', gmmktime($offset, gmdate('i'), gmdate('s'), gmdate('m'), gmdate('d'), gmdate('Y')));
	for($i = 0; $i < 24 ; $i++) {
		$i = str_pad($i,2,'0',STR_PAD_LEFT);
		$h = gmdate('G', gmmktime($i + $offset, gmdate('i'), gmdate('s'), gmdate('m'), gmdate('d'), gmdate('Y')));
		if($h >= $midnight) {
			$hours[$h] = ($i + 24);
		} else {
			$hours[$h] = ($i);
		}
	}
	ksort($hours);
	foreach($hours as $key => $val) {
		if($key == 0) {
			$key = 12;
		} elseif($key > 12) {
			$key = $key - 12;
		}
		if(gmdate('G', $the_post['posted_date_raw']) == $val || (gmdate('G') == $val && !$the_post)) {
			$dropdowns .= "<option value =\"$val\" selected>$key</option>";
		} else {
			$dropdowns .= "<option value =\"$val\">$key</option>";
		}
	}
	$dropdowns .= "</select></span>\n";
	// The Minute
	$dropdowns .= "<select name=\"" . $name_prefix . "date_min\" id=\"" . $name_prefix . "date_min_dropdown\" class=\"date_select\">";
	for($i = 0;$i < 60;$i++) {
		$i = str_pad($i,2,'0',STR_PAD_LEFT);
		$m = gmdate('i', gmmktime(gmdate('H') + $offset, $i, gmdate('s'), gmdate('m'), gmdate('d'), gmdate('Y')));
		if(gmdate('i', $the_post['posted_date_raw']) == $i || (gmdate('i') == $i && !$the_post)) {
			$dropdowns .= "<option value=\"$i\" selected>$m</option>";
		} else {
			$dropdowns .= "<option value=\"$i\">$m</option>";
		}
	}
	$dropdowns .= "</select>\n";
	// The Meridian
	$dropdowns .= "<select name=\"" . $name_prefix . "date_meridian\" id=\"" . $name_prefix . "date_meridian_dropdown\" class=\"date_select\">";
	if($the_post) {
		$m = gmdate('A', $the_post['posted_date_raw']);
	} else {
		$m = gmdate('A', gmmktime(gmdate('H') + $offset, gmdate('i'), gmdate('s'), gmdate('m'), gmdate('d'), gmdate('Y')));
	}
	if($m == 'AM') {
		$dropdowns .= "<option value=\"am\" selected>AM</option>";
	} else {
		$dropdowns .= "<option value=\"am\">AM</option>";
	}
	if($m == 'PM') {
		$dropdowns .= "<option value=\"pm\" selected>PM</option>";
	} else {
		$dropdowns .= "<option value=\"pm\">PM</option>";
	}
	$dropdowns .= "</select>\n";

	// The Seconds (hidden)
	$dropdowns .= "<input type=\"hidden\" name=\"" . $name_prefix . "date_secs\" value=\"" . gmdate('s', $the_post['posted_date_raw']) . "\" />\n";

	return $dropdowns;
}

function drizzo_google_sitemap() {
	global $drizzo_posts;
	drizzo_db_connect();
	$posts_lookup = @mysql_query("SELECT `ID`,`posted_date`,`page_parent` FROM `$drizzo_posts` WHERE `status`='publish'");
	if(!mysql_errno()) {
		while($post = mysql_fetch_assoc($posts_lookup)) {
			$links[$i] = array(
				"loc" => drizzo_post_permalink($post['ID']),
				"lastmod" => gmdate("Y-m-d\TH:i:s\Z",$post['posted_date']),
				"changefreq" => "daily"
				);
			if($page['page_parent'] === 0) {
				$links[$i]['priority'] = 1.0;
			} elseif((time() - $post['posted_date']) < (30 * 24 * 60 * 60)) {
				$links[$i]['priority'] = 0.7;
			} else {
				$links[$i]['priority'] = 0.7;
			}
			$i++;
		}
	}
	$static_items = array('blog','archive','podcasts','gallery');
	$optional_static_items = explode(",",drizzo_global_options('sitemap_addons'));
	$static_items = array_merge($static_items, $optional_static_items);
	foreach($static_items as $item) {
		$links[$i]['loc'] = domain . $item;
		if(array_search($item, $optional_static_items)) {
			$links[$i]['lastmod'] = gmdate("Y-m-d\TH:i:s\Z", filectime(root_dir . $item));
			$links[$i]['changefreq'] = "never";
			$links[$i]['priority'] = 0.5;
		} else {
			$links[$i]['lastmod'] = gmdate("Y-m-d\TH:i:s\Z");
			$links[$i]['changefreq'] = "always";
			$links[$i]['priority'] = 1.0;
		}
		$i++;
	}
	$sitemap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	$sitemap .= "\t<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\">\n";
	foreach($links as $link) {
		$sitemap .= "\t\t<url>\n";
		foreach($link as $tag => $value) {
			$sitemap .= "\t\t\t<$tag>$value</$tag>\n";
		}
		$sitemap .= "\t\t</url>\n";
	}
	$sitemap .= "\t</urlset>";
	return $sitemap;
}

function drizzo_ping_pingomatic() {
	global $drizzo_options;
	$services_lookup = @mysql_query("SELECT `value` FROM `$drizzo_options` WHERE `name`='ping_service'");
	if(!mysql_error()) {
		while($service = mysql_fetch_assoc($services_lookup)) {
			$services[] = $service['value'];
		}
		$service = null;
		if(count($services) > 0) {
			$domain = domain;
			$ping = "http://pingomatic.com/ping/?title=" . drizzo_global_options('site_name') . "&amp;blogurl=" . rawurlencode($domain) . "&amp;rssurl=" . rawurlencode($domain) . "feed%2F";
			foreach($services as $service) {
				$ping .= "&amp;chk_$service=on";
			}
			$pingomatic = fopen($ping,'r');
			fclose($pingomatic);
			if($pingomatic) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	} else {
		return false;
	}
}

if(!function_exists('ftp_chmod')) { // From http://us3.php.net/manual/en/function.ftp-chmod.php
	function ftp_chmod($ftp_stream, $mode, $filename) {
		return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
	}
}

function drizzo_mkdir($directories) {	
	if(ini_get('safe_mode')) {
		$ftp_connect	=	ftp_connect(drizzo_global_options('ftp_connect'));
		$ftp_user		=	drizzo_global_options('ftp_user');
		$ftp_pass		=	drizzo_global_options('ftp_pass');
		if(@ftp_login($ftp_connect,$ftp_user,$ftp_pass)) {
			ftp_pasv($ftp_connect,true);

			$ftp_root_directory			=	ftp_pwd($ftp_connect);
			if(ftp_nlist($ftp_connect,$ftp_root_directory)) {
				foreach(ftp_nlist($ftp_connect,$ftp_root_directory) as $ftp_directory) {
					$ftp_root_directories[] = str_replace($ftp_root_directory . "/",'',$ftp_directory);
				}
			}
			
			$server_root_directories	=	explode("/", root_dir);
			array_shift($server_root_directories);
			array_pop($server_root_directories);
			
			$match = array_search($server_root_directories[count($server_root_directories) - 1], $ftp_root_directories);

			$root_directories	 =	$ftp_root_directory;
			$root_directories	.=	"/" . $ftp_root_directories[$match];
			
			for($i = array_search($ftp_root_directories[$match], $server_root_directories) + 1; $i < count($server_root_directories); $i++) {
				$root_directories .= "/" . $server_root_directories[$i];
			}
			$directories 		=	explode("/",str_replace(root_dir,$root_directories . "/",$directories));
			$root_directories	=	explode("/", $root_directories);
			foreach($directories as $i => $directory) {
				if(!in_array($directory,$root_directories)) {
					$parents = null;
					for($x = 0;$x < $i;$x++) {
						$parents .= "/" . $directories[$x];
					}
					if(@ftp_chdir($ftp_connect,"$parents/$directory") === false) {
						ftp_mkdir($ftp_connect,"$parents/$directory");
						ftp_chmod($ftp_connect,0777,"$parents/$directory");
					}
				}
			}
			ftp_close($ftp_connect);
		}
	} else {
		$directories 		=	explode("/",$directories);
		array_shift($directories);

		$root_directories	=	explode("/", root_dir);
		array_shift($root_directories);

		foreach($directories as $i => $directory) {
			if(!in_array($directory,$root_directories)) {
				$parents = null;
				for($x = 0;$x < $i;$x++) {
					$parents .= "/" . $directories[$x];
				}
				if(!file_exists("$parents/$directory")) {
					mkdir("$parents/$directory", 0777);
				}
			}
		}
	}
}
?>
